
using System;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_ORDER.DBAccess
{

	public class OrderDeliveryDAO : BaseDAO
	{
        #region Basic Method

        public static DataTable GetAll()
        {
            return ExcuteProcedure(OrderConstants.StoreProcedure.OrderDelivery.GetAll);
        }

        public static DataTable GetOne(string orderId)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
						CreateParameter("@OrderID", SqlDbType.NVarChar, orderId)
                    };
                return ExcuteProcedure(OrderConstants.StoreProcedure.OrderDelivery.GetOne, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }


        public static OrderDelivery LoadOne(string orderId)
        {
            OrderDelivery o = new OrderDelivery();
            DataTable dt = GetOne(orderId);
            if (dt.Rows.Count > 0)
            {
                //o.ID = int.Parse(dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.ID].ToString());
                o.OrderID = dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.OrderID].ToString();
                o.Date = DateTime.Parse(dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.Date].ToString());
                o.ProductID = dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.ProductID].ToString();
                o.Deadline = DateTime.Parse(dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.Deadline].ToString());
                o.Quantity = int.Parse(dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.Quantity].ToString());
                o.Status = dt.Rows[0][OrderConstants.Entities.OrderDelivery.FieldName.Status].ToString();

            }

            return o;
        }


        public static int Insert(OrderDelivery o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@Deadline", SqlDbType.DateTime, o.Deadline),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Status", SqlDbType.NVarChar, o.Status),
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderDelivery.Insert, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static int Update(OrderDelivery o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ProductID", SqlDbType.NVarChar, o.ProductID),
                        CreateParameter("@Deadline", SqlDbType.DateTime, o.Deadline),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Status", SqlDbType.NVarChar, o.Status),
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderDelivery.Update, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static int Delete(OrderDelivery o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.OrderDelivery.Delete, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static DataTable GetTemplateTable()
        {
            DataTable table = new DataTable("243_Order");

            return table;
        }


        #endregion
    }
	
}
